#include <ants.hpp>
#include <function.hpp>
using namespace ants;
TObject *TObject::getParent(){
    return this->parent;
}
void TObject::setParent(TObject *parent){
    this->parent = parent;
}
void TObject::addChild(TObject *child){
    this->children.insert(child);
}
void TObject::removeChild(TObject *child){
    this->children.erase(child);
}
const std::vector<TObject *> &TObject::getChildren() const{
    std::vector<TObject *> *children = new std::vector<TObject *>();
    for(auto it : this->children){
        children->push_back(it);
    }
    return *children;
}
void TObject::addHandler(const std::string &name, std::function<void(void)> handler){
    int64_t hash = hash1(name.c_str()) + hash2(name.c_str());
    this->handlers[hash] = handler;
}
void TObject::removeHandler(const std::string &name){
    int64_t hash = hash1(name.c_str()) + hash2(name.c_str());
    this->handlers.erase(hash);
}
const std::function<void(void)> &TObject::getHandler(const std::string &name) const{
    static const std::function<void(void)> nullHandler = ([](){
        fprintf(stderr, "这是一个空句柄\n");
    });
    int64_t hash = hash1(name.c_str()) + hash2(name.c_str());
    std::unordered_map<int64_t, std::function<void(void)>>::const_iterator it = this->handlers.find(hash);
    if(it == this->handlers.end())
        return nullHandler;
    return it->second;
}
TObject::~TObject(){
    this->parent = nullptr;
    this->children.clear();
    this->handlers.clear();
}
